package com.lyshop.guiyingshanding.adapters;

import android.content.Context;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.lyshop.guiyingshanding.R;
import com.lyshop.guiyingshanding.base.BaseRVAdapter;
import com.lyshop.guiyingshanding.base.BaseRVHolder;
import com.lyshop.guiyingshanding.base.BaseViewHolder;
import com.lyshop.guiyingshanding.bean.CalendarBean;
import com.lyshop.guiyingshanding.utils.GlideUtils;

import java.util.List;


public class EconomicsAdapter extends BaseRVAdapter<CalendarBean> {

    private ImageView iv_country;
    private TextView tv_time;
    private TextView tv_result;
    private TextView tv_title;
    private TextView tv_before;
    private TextView tv_forecast;
    private TextView tv_current;
    private RatingBar ratingBar;

    public EconomicsAdapter(Context context, List<CalendarBean> list) {
        super(context, list);
    }

    @Override
    public int onCreateViewLayoutID(int viewType) {
        return R.layout.item_home_economics;
    }

    @Override
    public void onBindView(BaseRVHolder mHolder, int position) {
        BaseViewHolder holder = mHolder.getViewHolder();
        iv_country = holder.get(R.id.iv_country);
        tv_time = holder.get(R.id.tv_time);
        ratingBar = holder.get(R.id.ratingBar);
        tv_title = holder.get(R.id.tv_title);
        tv_result = holder.get(R.id.tv_result);
        tv_before = holder.get(R.id.tv_before);
        tv_forecast = holder.get(R.id.tv_forecast);
        tv_current = holder.get(R.id.tv_current);

        CalendarBean bean = mList.get(position);
        if (bean != null) {
            GlideUtils.showImageOptions(mContext, bean.flag_url, iv_country);
            tv_time.setText(bean.pub_time);
            ratingBar.setRating(bean.star);
            tv_title.setText(bean.country + bean.time_period + bean.indicator_name);

            if (TextUtils.isEmpty(bean.actual)){
                tv_result.setText("未公布");
                tv_result.setBackgroundResource(R.color.color_gray_15);
                tv_result.setTextColor(ContextCompat.getColor(mContext, R.color.color_gray_02));
            }else {
                if (bean.affect == 0) {
                    if (bean.consensus == bean.actual){
                        tv_result.setText("影响较小");
                    }else {
                        tv_result.setText("利多");
                    }
                    tv_result.setBackgroundResource(R.color.color_red_17);
                    tv_result.setTextColor(ContextCompat.getColor(mContext, R.color.color_red_16));
                } else if (bean.affect == 1) {
                    tv_result.setText("利空");
                    tv_result.setBackgroundResource(R.color.color_green_08);
                    tv_result.setTextColor(ContextCompat.getColor(mContext, R.color.color_green_07));
                }
            }
            String unit = TextUtils.isEmpty(bean.unit) ? "" : bean.unit.equals("%") ? "%" : "";
            tv_before.setText("前值：" + bean.previous + unit);
            tv_forecast.setText("预测：" + (TextUtils.isEmpty(bean.consensus) ? "--" : (bean.consensus + unit)));
            tv_current.setText("公布：" + (TextUtils.isEmpty(bean.actual) ? "--" : (bean.actual + unit)));
        }
    }
}
